package com.yhserver.yhserver1.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @ClassName: WebHooksController.java
 * @author: yuhui
 * @version: 1.0.0
 * @Description: config全自动刷新控制器
 * @createTime: 2021年07月25日 15:00:00
 */
@RestController
public class WebHooksController {

    @Autowired
    private RestTemplate restTemplate;

    @PostMapping("refreshAll")
    public ResponseEntity<String> refreshConfig(){
        HttpHeaders headers = new HttpHeaders();
        headers.add("content-type","application/json;charset=utf-8");

        HttpEntity<String> entity = new HttpEntity<>(headers);
        String url = "http://pre1:8761/actuator/bus-refresh";
        ResponseEntity<String> repEntity = restTemplate.postForEntity(url, entity, String.class);
        int value = repEntity.getStatusCode().value();
        if(value == HttpStatus.NO_CONTENT.value()){
            return ResponseEntity.ok("刷新成功");
        }
        return ResponseEntity.badRequest().body("刷新失败");
    }
}
